from flask import Blueprint,json,jsonify,g
from flask_restful import reqparse,Api,Resource,marshal
from models.courses import *
from models.users import *
from common.utlis.my_output_json import custom_output_json
from common.utlis.name_list import *
from common.utlis.login_utlis import login_required

comment_bp = Blueprint('comment', __name__, url_prefix='/comment')

api = Api(comment_bp)

class CommentInfo(Resource):
    # 获取所有的评论信息
    def get(self):
        try:
            parser = reqparse.RequestParser()
            parser.add_argument('course_id')
            args = parser.parse_args()
            course_id = args.get('course_id')

            comment = Comments.query.filter_by(course=course_id).all()
            comment = marshal(comment,comment_fields)
            comment = json.loads(json.dumps(comment))
            print('评论', comment)
            for i in comment:
                user = User.query.filter_by(id=i['user']).first()
                user = marshal(user,user_fields)
                i['user_info'] = user

                comment_data = Comments.query.filter_by(parent_id=i['id']).all()
                comment_data = marshal(comment_data,comment_fields)
                comment_data = json.loads(json.dumps(comment_data))

                for j in comment_data:
                    user = User.query.filter_by(id=j['user']).first()
                    user = marshal(user, user_fields)
                    j['user_info'] = user

                i['childlist'] = comment_data
            return jsonify(message='获取评论成功', code=200, data=comment)
        except Exception as e:
            print('错误', e)
            return jsonify(message='获取评论失败', code=400)

    # 判断用户是否登录
    @login_required
    def post(self):
        try:
            parser = reqparse.RequestParser()
            parser = parser.add_argument('content')
            parser = parser.add_argument('cid')
            parser = parser.add_argument('reply')

            args = parser.parse_args()
            content = args.get('content')
            cid = args.get('cid')
            parent_id = args.get('reply')
            print('发表的评论内容:', content,cid,parent_id)

            user_id = g.user_id
            if not user_id:
                return jsonify(message='请重新登录', code=401)

            if cid != None:
                comment = Comments(user=user_id,course=cid,comment_content=content)
            else:
                comment = Comments(user=user_id,parent_id=parent_id,comment_content=content)

                comment_one = Comments.query.filter_by(id=parent_id).first()

                if not comment_one:
                    return jsonify(message='没有此评论', code=401)

                # 更新回复的数量
                Comments.query.filter_by(id=parent_id).update({'reply_count':comment_one.reply_count+1})
                db.session.commit()

            db.session.add(comment)
            db.session.commit()
            return jsonify(message='发表评论成功', code=200)
        except Exception as e:
            print('错误>>>', e)
            return jsonify(message='发表评论失败', code=400)


# 评论点赞
class CommentLike(Resource):
    def post(self):
        try:
            parser = reqparse.RequestParser()
            parser.add_argument('com_id')
            parser.add_argument('is_favorite')
            args = parser.parse_args()

            com_id = args.get('com_id')
            is_favorite = args.get('is_favorite')
            print('获取评论的id>>', com_id)
            print('获取状态>>',is_favorite, type(is_favorite))

            comment = Comments.query.filter_by(id=com_id).first()
            print('获取的评论>>>>>', comment)
            if not comment:
                return jsonify(message='没有该评论', code=204)

            comment_list = Comments.query.filter_by(id=com_id)
            if is_favorite == "True":
                comment_list.update({'zan_num':comment.zan_num + 1})
                db.session.commit()
                return jsonify(message='点赞成功', code=200)
            else:
                comment_list.update({'zan_num':comment.zan_num - 1})
                db.session.commit()
                return jsonify(message='取消点赞成功', code=200)


        except Exception as e:
            print('错误>>', e)
            return jsonify(message='点赞失败', code=500)


# 获取评论信息
api.add_resource(CommentInfo, '/get_comment')

# 点赞评论
api.add_resource(CommentLike, '/favorite_comment')